<script setup lang="ts">
import { getOrderList } from '@/services/order'
import type { OrderItem, OrderListParams, OrderListResult } from '@/types/order'
import { ref, defineProps } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { orderStateList, OrderState } from '@/services/constants'
import { getWXMockpay, getWXpay } from '@/services/pay'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

const props = defineProps<{
  orderState: number
}>()
// 请求参数
const params: OrderListParams = {
  page: 1,
  pageSize: 5,
  orderState: props.orderState,
}

// 获取订单列表
const orderList = ref<OrderItem[]>([])
const getOrderListData = async () => {
  if (finish.value) {
    uni.showToast({ icon: 'none', title: '没有更多了' })
    return
  }
  const res = await getOrderList(params)
  // 判断是否还有数据
  if (params.page! < res.result.pages) {
    params.page = params.page! + 1
  } else {
    finish.value = true
  }
  orderList.value?.push(...res.result.items)
}
// 下拉触底
const onScrolltolower = () => {
  getOrderListData()
}

// 是否还能上拉加载
const finish = ref(false)

// 支付
const goPay = async (id: string) => {
  // 微信生产环境支付
  if (import.meta.env.PROD) {
    const res = await getWXpay(id)
    // 唤醒支付窗口
    await wx.requestPayment(res.result)
  } else if (import.meta.env.DEV) {
    await getWXMockpay(id)
  }
  uni.showToast({ icon: 'none', title: '支付成功' })
  // 支付成功
  const order = orderList.value.find((v) => v.id == id)
  order!.orderState = OrderState.DaiFaHuo
}

onLoad(() => {
  getOrderListData()
})
</script>

<template>
  <!-- 订单列表 -->
  <scroll-view scroll-y class="orders" @scrolltolower="onScrolltolower">
    <view class="card" v-for="item in orderList" :key="item.id">
      <!-- 订单信息 -->
      <view class="status">
        <text class="date">{{ item.createTime }}</text>
        <!-- 订单状态文字 -->
        <text>{{ orderStateList[item.orderState].text }}</text>
        <!-- 待评价/已完成/已取消 状态: 展示删除订单 -->
        <text v-if="item.orderState >= OrderState.DaiPingJia" class="icon-delete"></text>
      </view>
      <!-- 商品信息，点击商品跳转到订单详情，不是商品详情 -->
      <navigator
        v-for="sku in item.skus"
        :key="sku.id"
        class="goods"
        :url="`/pagesOrder/orderDetail/orderDetail?id=${item.id}`"
        hover-class="none"
      >
        <view class="cover">
          <image mode="aspectFit" :src="sku.image"></image>
        </view>
        <view class="meta">
          <view class="name ellipsis">{{ sku.name }}</view>
          <view class="type">{{ sku.attrsText }}</view>
        </view>
      </navigator>
      <!-- 支付信息 -->
      <view class="payment">
        <text class="quantity">共{{ item.totalNum }}件商品</text>
        <text>实付</text>
        <text class="amount"> <text class="symbol">¥</text>{{ item.payMoney }}</text>
      </view>
      <!-- 订单操作按钮 -->
      <view class="action">
        <!-- 待付款状态：显示去支付按钮 -->
        <template v-if="item.orderState == OrderState.DaiFuKuan && item.countdown != -1">
          <view class="button primary" @tap="goPay(item.id)">去支付</view>
        </template>
        <template v-else>
          <navigator
            class="button secondary"
            :url="`/pagesOrder/order/order?orderId=${item.id}`"
            hover-class="none"
          >
            再次购买
          </navigator>
          <!-- 待收货状态: 展示确认收货 -->
          <view v-if="item.orderState == OrderState.DaiShouHuo" class="button primary"
            >确认收货</view
          >
        </template>
      </view>
    </view>
    <!-- 底部提示文字 -->
    <view class="loading-text" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }">
      {{ finish ? '没有更多数据~' : '正在加载...' }}
    </view>
  </scroll-view>
</template>
